
from PIL import Image
import numpy as np
import cv2
import imagehash

def compare_images(image_path1, image_path2):
    # 加载图片
    image1 = Image.open(image_path1)
    image2 = Image.open(image_path2)

    # 转换为灰度图
    gray_image1 = image1.convert("L")
    gray_image2 = image2.convert("L")




    hist1 = cv2.calcHist([np.array(gray_image1)], [0], None, [256], [0, 256])
    hist2 = cv2.calcHist([np.array(gray_image2)], [0], None, [256], [0, 256])

    hist_diff = cv2.compareHist(hist1, hist2, cv2.HISTCMP_CORREL)
    print(hist_diff)

    phash1 = imagehash.phash(gray_image1)
    phash2 = imagehash.phash(gray_image2)
    hash_diff = phash1 - phash2
    print(hash_diff)
    return hash_diff



if __name__ == '__main__':
    print('PyCharm')
    # 使用示例
    # correlation = compare_histograms('D:/FaceDetect/1.jpg', 'D:/FaceDetect/2.jpg')
    # print(f"Histogram correlation: {correlation}")
    # 使用示例
    # num_matches = compare_features('D:/FaceDetect/1.jpg', 'D:/FaceDetect/2.jpg')
    # print(f"Number of feature matches: {num_matches}")
    # 使用示例
    similarity = compare_images('D:/FaceDetect/1.jpg', 'D:/FaceDetect/2.jpg')
    print(f"Image similarity: {similarity}")